package com.vinobase.model.dao;


import com.googlecode.objectify.NotFoundException;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyFactory;
import com.vinobase.authentication.UserRegistry;
import com.vinobase.model.bean.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;


public class UserDao implements UserRegistry {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ObjectifyFactory objectifyFactory;

    public User findUser(String userId) {

        try{
        Objectify ofy = objectifyFactory.begin();
        User user = ofy.get(User.class, userId);
        return user;
        } catch (NotFoundException e){
             logger.debug(userId + " not found in datastore");
            return null;
        }


    }

    public void registerUser(User newUser) {

        Objectify ofy = objectifyFactory.begin();
        ofy.put(newUser);

    }

    public void removeUser(String userId) {
        Objectify ofy = objectifyFactory.begin();
        ofy.delete(User.class, userId);


    }



    public void setObjectifyFactory(ObjectifyFactory objectifyFactory) {
        this.objectifyFactory = objectifyFactory;
    }

    public ObjectifyFactory getObjectifyFactory() {
        return objectifyFactory;
    }
}
